from def_pack.Tkkinter_widget import CreatNormalWidget
import tkinter
from functools import partial

from def_pack.image_show_frame_program import image_pdf_fitle_program, pdf_view_rename_program
from def_pack.recover_data import RecoverData3
# import os

def choice_filter_way(fr1,fr2,fr3,main_win,cho_num=0):
    tframe1 = fr1.get("1.0", "end").replace("\n", "")
    picture_dir = str(tframe1).replace("\\", "/")

    redata = RecoverData3()
    textframelist = [fr1, fr2, fr3]
    redata.write_file_from_get_frame(textframelist)

    if len(picture_dir) < 4:
        print("没有输入视频路径，请输入视频路径再尝试")
        return
    if cho_num == 0:
        bc_volume = fr2.get("1.0", "end").replace("\n", "")
        ma_audio_volume = fr3.get("1.0", "end").replace("\n", "")
        if len(bc_volume) <1 or len(ma_audio_volume) <1:
            print("没有输入上边界和每行边界，请输入后再尝试")
            return

        main_win.destroy()
        image_pdf_fitle_program(picture_dir, int(bc_volume), int(ma_audio_volume))
    if cho_num == 1:
        main_win.destroy()
        pdf_view_rename_program(picture_dir)


class PdfPictureFilter():
    def __init__(self):
        self.classb = CreatNormalWidget()
        self.main_win = self.classb.creat_main_win("筛选pdf式图片", "picture.ico")
        self.textframe = self.classb.create_textframe_show2(self.main_win, 2, 1, 80, 4, columnspan=8)
        self.textframe3 = self.classb.create_textframe_show3(self.main_win, 3, 2, 12, 2, columnspan=1)
        self.textframe4 = self.classb.create_textframe_show3(self.main_win, 3, 4, 15, 2, columnspan=2)
        # self.textframe5 = self.classb.create_textframe_show3(self.main_win, 3, 4, 15, 2, columnspan=2)

        self.lable = self.classb.creat_lable(self.main_win, "图片所在文件夹", 1, 0, columnspan=2)
        self.lable3 = self.classb.creat_lable(self.main_win, "上边界", 3, 1, columnspan=1)
        self.lable4 = self.classb.creat_lable(self.main_win, "每行边界", 3, 3, columnspan=1)

        self.__show_button()
        self.__get_tempframe_data()
        self.main_win.mainloop()

    # 创建一个按钮,这书蓝底的按钮
    def __create_button(self,main_win, h_numa, l_colu_numa, text1, command, width1=6, height1=1, bg="Wheat", columnspan=1):
        if command is None:
            btn1 = tkinter.Button(main_win, text=f"{text1}", width=width1, height=height1,
                                  # padx=10, pady=10,
                                  bg=bg, font=("楷书", 11), fg="MediumBlue",
                                  )

        else:
            btn1 = tkinter.Button(main_win, text=f"{text1}", width=width1, height=height1,
                                  # padx=10, pady=10,
                                  bg=bg, font=("楷书", 11), fg="MediumBlue",
                                  # LightCyan，MediumBlue
                                  command=command)
        btn1.grid(row=h_numa, column=l_colu_numa, padx=5, pady=10, columnspan=columnspan)
        return btn1, h_numa, l_colu_numa

    def __show_button(self):
        self.search_button = self.__create_button(self.main_win, 4, 2, "PDF图片筛选",
                                           partial(choice_filter_way, self.textframe[0], self.textframe3[0],
                                                   self.textframe4[0], self.main_win, cho_num=0),
                                           height1=1, width1=18,
                                           bg="LightCyan")
        self.search_button = self.__create_button(self.main_win, 4, 3, "重命名PDF图片",
                                                  partial(choice_filter_way, self.textframe[0], self.textframe3[0],
                                                          self.textframe4[0], self.main_win, cho_num=1),
                                                  height1=1, width1=18,
                                                  bg="LightCyan")
    def __get_tempframe_data(self):
        listoo = [self.textframe[0], self.textframe3[0], self.textframe4[0]]
        redata=RecoverData3()
        redata.write_data(listoo)
if __name__ == '__main__':
    PdfPictureFilter()
    pass